package com.audi.handler;

import java.util.ArrayList;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.audi.dto.SmartTriggerItem;
import com.audi.dto.SmartTriggerTriggerItem;
import com.audi.dto.SmartTriggerWhenItem;
import com.audi.dto.SmartTriggerWhenTriggerItem;

/**
 * @author PhongNT11
 * 
 */
public class SmartTriggerHandler extends Handler {
	/**
	 * processMessage
	 * 
	 * @param actionId
	 *            int
	 * @param data
	 *            String
	 * @return Message
	 * @see message.arg1 = 0 if return successful, message.arg1 = -1 if fail
	 */
	public Message process(int actionId, String data) {
		Message message = new Message();
		Bundle dataBundle = new Bundle();
		message.arg1 = 0;
		String key = String.valueOf(actionId);
		try {
			switch (actionId) {
			// resultAddMember
			case ADD_WHEN_ST:
			case ADD_TRIGGER_ST:
			case ADD_WHEN_TRIGGER_ST:
			case UPDATE_WHEN_ST:
			case UPDATE_TRIGGER_ST:
			case UPDATE_WHEN_TRIGGER_ST:
			case DELETE_WHEN_ST:
			case DELETE_TRIGGER_ST:
			case DELETE_WHEN_TRIGGER_ST:
				dataBundle.putInt(key, Integer.valueOf(data));
				break;
			case GET_WHEN_ST_BY_ID:
				dataBundle.putSerializable(key, json2ListWhenST(data));

				break;
			case GET_TRIGGER_ST_BY_ID:
				dataBundle.putSerializable(key, json2ListTriggerST(data));
				break;
			case GET_WHEN_TRIGGER_ST_BY_ID:
				dataBundle.putSerializable(key, json2ListWhenTriggerST(data));
				break;
			case GET_ALL_ST:
				dataBundle.putSerializable(key, json2ListST(data));
				break;
			case GET_LIST_WHEN_ST:
				dataBundle.putSerializable(key, json2ListWhenST(data));
				break;
			case GET_LIST_TRIGGER_ST:
				dataBundle.putSerializable(key, json2ListTriggerST(data));
				break;
			case GET_LIST_WHEN_TRIGGER_ST:
				dataBundle.putSerializable(key, json2ListWhenTriggerST(data));
				break;
			case SET_ON_OFF_ST:
			case GET_DEVICE_FROM_WHEN_ST:
			case GET_DEVICE_FROM_TRIGGER_ST:
				dataBundle.putInt(key, Integer.valueOf(data));
				break;
			default:
				break;
			}
		} catch (Exception e) {
			Log.e("Client", "[SmartTriggerHandler.java]: " + e.toString());
			message.arg1 = -1;
		}
		message.setData(dataBundle);
		return message;
	}

	public ArrayList<SmartTriggerItem> json2ListST(String data) {
		ArrayList<SmartTriggerItem> result = (ArrayList<SmartTriggerItem>) convertJson2List(
				data, SmartTriggerItem.class);
		return result;
	}

	public ArrayList<SmartTriggerWhenItem> json2ListWhenST(String data) {
		ArrayList<SmartTriggerWhenItem> result = (ArrayList<SmartTriggerWhenItem>) convertJson2List(
				data, SmartTriggerWhenItem.class);
		return result;
	}

	public ArrayList<SmartTriggerTriggerItem> json2ListTriggerST(String data) {
		ArrayList<SmartTriggerTriggerItem> result = (ArrayList<SmartTriggerTriggerItem>) convertJson2List(
				data, SmartTriggerTriggerItem.class);
		return result;
	}

	public ArrayList<SmartTriggerWhenTriggerItem> json2ListWhenTriggerST(
			String data) {
		ArrayList<SmartTriggerWhenTriggerItem> result = (ArrayList<SmartTriggerWhenTriggerItem>) convertJson2List(
				data, SmartTriggerWhenTriggerItem.class);
		return result;
	}
}
